package com.hardy.newSolutionAlgorithm;

/**
 * Author: fuxh3@lenovo.com
 * Date: Created in 2017/11/10 0010
 * 最小可用ID
 */
public class MinAvailableId {
    public MinAvailableId(){

    }

    public static void main(String[] args) {
        int[] arr = {18,4,8,9,16,1,14,7,19,3,0,5,2,11,6};
        System.out.println(method1(arr,arr.length));
        System.out.println(method2(arr,arr.length));
    }

    public static int method1(int[] nums,int n){
        int i=0;
        while (true){
               if(!isExist(i,nums,n)){
                   return i;
               }else{
                   i=i+1;
               }
        }
    }

    private static boolean isExist(int i, int[] nums, int n) {
        for(int var =0;var<n;var++){
            if(nums[var]==i){
                return true;
            }
        }
        return false;
    }

    private static int method2(int[] nums,int n){
        boolean[] flag = new boolean[n+1];

        for (int i=0;i<n+1;i++){
            flag[i]=false;
        }
        for(int j=0;j<n;j++){
            if(nums[j]<n){
                flag[nums[j]]=true;
            }
        }
        for(int var =0;var<n;var++){
            if(flag[var]==false){
                return var;
            }
        }
        for (int k=0;k<n;k++){
            System.out.println(flag[k]);
        }
        return 0;
    }
}
